<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="叶片所处植株编号" prop="plantNumber">
        <el-input
          v-model="queryParams.plantNumber"
          placeholder="请输入叶片所处植株编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶位" prop="leafPlace">
        <el-input
          v-model="queryParams.leafPlace"
          placeholder="请输入叶位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶面积" prop="leafArea">
        <el-input
          v-model="queryParams.leafArea"
          placeholder="请输入叶面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶干重" prop="leafDryWeight">
        <el-input
          v-model="queryParams.leafDryWeight"
          placeholder="请输入叶干重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶片氮含量" prop="leafNitrogenContent">
        <el-input
          v-model="queryParams.leafNitrogenContent"
          placeholder="请输入叶片氮含量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶绿素a含量" prop="chlorophyllAContent">
        <el-input
          v-model="queryParams.chlorophyllAContent"
          placeholder="请输入叶绿素a含量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶绿素b含量" prop="chlorophyllBContent">
        <el-input
          v-model="queryParams.chlorophyllBContent"
          placeholder="请输入叶绿素b含量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="叶绿素a+b含量" prop="chlorophyllABContent">
        <el-input
          v-model="queryParams.chlorophyllABContent"
          placeholder="请输入叶绿素a+b含量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测量日期" prop="measurementDate">
        <el-date-picker clearable
          v-model="queryParams.measurementDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择测量日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="棉花品种" prop="variety">
        <el-input
          v-model="queryParams.variety"
          placeholder="请输入棉花品种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后修改时间" prop="lastEditTime">
        <el-date-picker clearable
          v-model="queryParams.lastEditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="叶片备注" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入叶片备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vbze:leaf:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vbze:leaf:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vbze:leaf:remove']"
        >删除</el-button>
      </el-col>



      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['vbze:leaf:import']"
        >导入</el-button>
      </el-col>



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vbze:leaf:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leafList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="叶片ID" align="center" prop="id" />
      <el-table-column label="叶片所处植株编号" align="center" prop="plantNumber" />
      <el-table-column label="叶位" align="center" prop="leafPlace" />
      <el-table-column label="叶面积" align="center" prop="leafArea" />
      <el-table-column label="叶干重" align="center" prop="leafDryWeight" />
      <el-table-column label="叶片氮含量" align="center" prop="leafNitrogenContent" />
      <el-table-column label="叶绿素a含量" align="center" prop="chlorophyllAContent" />
      <el-table-column label="叶绿素b含量" align="center" prop="chlorophyllBContent" />
      <el-table-column label="叶绿素a+b含量" align="center" prop="chlorophyllABContent" />
      <el-table-column label="测量日期" align="center" prop="measurementDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.measurementDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="棉花品种" align="center" prop="variety" />
      <el-table-column label="叶片备注" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vbze:leaf:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vbze:leaf:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改叶片管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="叶片所处植株编号" prop="plantNumber">
          <el-input v-model="form.plantNumber" placeholder="请输入叶片所处植株编号" />
        </el-form-item>
        <el-form-item label="叶位" prop="leafPlace">
          <el-input v-model="form.leafPlace" placeholder="请输入叶位" />
        </el-form-item>
        <el-form-item label="叶面积" prop="leafArea">
          <el-input v-model="form.leafArea" placeholder="请输入叶面积" />
        </el-form-item>
        <el-form-item label="叶干重" prop="leafDryWeight">
          <el-input v-model="form.leafDryWeight" placeholder="请输入叶干重" />
        </el-form-item>
        <el-form-item label="叶片氮含量" prop="leafNitrogenContent">
          <el-input v-model="form.leafNitrogenContent" placeholder="请输入叶片氮含量" />
        </el-form-item>
        <el-form-item label="叶绿素a含量" prop="chlorophyllAContent">
          <el-input v-model="form.chlorophyllAContent" placeholder="请输入叶绿素a含量" />
        </el-form-item>
        <el-form-item label="叶绿素b含量" prop="chlorophyllBContent">
          <el-input v-model="form.chlorophyllBContent" placeholder="请输入叶绿素b含量" />
        </el-form-item>
        <el-form-item label="叶绿素a+b含量" prop="chlorophyllABContent">
          <el-input v-model="form.chlorophyllABContent" placeholder="请输入叶绿素a+b含量" />
        </el-form-item>
        <el-form-item label="测量日期" prop="measurementDate">
          <el-date-picker clearable
            v-model="form.measurementDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择测量日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="棉花品种" prop="variety">
          <el-input v-model="form.variety" placeholder="请输入棉花品种" />
        </el-form-item>
        <el-form-item label="最后修改人" prop="lastEditor">
          <el-input v-model="form.lastEditor" placeholder="请输入最后修改人" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastEditTime">
          <el-date-picker clearable
            v-model="form.lastEditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="叶片备注" prop="description">
          <el-input v-model="form.description" placeholder="请输入叶片备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的地块数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listLeaf, getLeaf, delLeaf, addLeaf, updateLeaf } from "@/api/vbze/leaf";
import { getToken } from '@/utils/auth'

export default {
  name: "Leaf",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 叶片管理表格数据
      leafList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的地块数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/vbze/leaf/importData"
      },




      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
        plantNumber: null,
        leafPlace: null,
        leafArea: null,
        leafDryWeight: null,
        leafNitrogenContent: null,
        chlorophyllAContent: null,
        chlorophyllBContent: null,
        chlorophyllABContent: null,
        measurementDate: null,
        variety: null,
        lastEditTime: null,
        description: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plantNumber: [
          { required: true, message: "叶片所处植株编号不能为空", trigger: "blur" }
        ],
        leafPlace: [
          { required: true, message: "叶位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询叶片管理列表 */
    getList() {
      this.loading = true;
      listLeaf(this.queryParams).then(response => {
        this.leafList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        creator: null,
        createTime: null,
        plantNumber: null,
        leafPlace: null,
        leafArea: null,
        leafDryWeight: null,
        leafNitrogenContent: null,
        chlorophyllAContent: null,
        chlorophyllBContent: null,
        chlorophyllABContent: null,
        measurementDate: null,
        variety: null,
        lastEditor: null,
        lastEditTime: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加叶片管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLeaf(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改叶片管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLeaf(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeaf(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除叶片管理编号为"' + ids + '"的数据项？').then(function() {
        return delLeaf(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vbze/leaf/export', {
        ...this.queryParams
      }, `leaf_${new Date().getTime()}.xlsx`)
    },



    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "叶片导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('vbze/leaf/importTemplate', {
      }, `leaf_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }


  }
};
</script>
